package Dao;

import Bean.BeanStudent;
import Bean.BeanUser;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void addStudent(BeanStudent student) throws Exception {
        String sql="insert into student(student_name,student_age,user_id) values(?,?,?)";
        this.jdbcTemplate.update(sql,new Object[] {student.getStudent_name(),student.getStudent_age(),student.getUser_id()});
    }
    public BeanStudent getStudent(int id) throws Exception {
        String sql="select student_id,student_name,student_age from student where student_id=?";
        BeanStudent student=this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanStudent>() {
            @Override
            public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                if(rs.next()) {
                    BeanStudent student1=new BeanStudent();
                    student1.setStudent_id(rs.getInt(1));
                    student1.setStudent_name(rs.getString(2));
                    student1.setStudent_age(rs.getInt(3));
                    return student1;
                }
                return null;
            }},id);
        return student;
    }
    public List<BeanStudent> getAllStudent() throws Exception {
        String sql="select student_id,student_name,student_age from student";
        List<BeanStudent> students=new ArrayList<>();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanStudent>() {
            @Override
            public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                while(rs.next()) {
                    BeanStudent student1=new BeanStudent();
                    student1.setStudent_id(rs.getInt(1));
                    student1.setStudent_name(rs.getString(2));
                    student1.setStudent_age(rs.getInt(3));
                    students.add(student1);
                }
                return null;
            }});
        return students;
    }
    public ArrayList<BeanStudent> getStudentsByUser(String id) throws Exception {
        String sql="select student_id,student_name,student_age from student where user_id=?";
        ArrayList<BeanStudent>students=new ArrayList<>();
        this.jdbcTemplate.query(sql, new ResultSetExtractor<BeanStudent>() {
            @Override
            public BeanStudent extractData(ResultSet rs) throws SQLException, DataAccessException {
                // TODO Auto-generated method stub
                while (rs.next()) {
                    BeanStudent student1=new BeanStudent();
                    student1.setStudent_id(rs.getInt(1));
                    student1.setStudent_name(rs.getString(2));
                    student1.setStudent_age(rs.getInt(3));
                    students.add(student1);
                }
                return null;
            }},id);
        return students;
    }
}
